<template>
  <div class="header-container">
    <!-- 顶部导航栏 -->
    <div class="top-navigation">
      <div class="container">
        <!-- 左侧导航链接 -->
        <div class="nav-links">
          <el-link
              v-for="link in navLinks"
              :key="link.name"
              :class="['nav-link', { active: activeLink === link.name }]"
              @click="handleNavClick(link.name, link.path)"
          >
            <el-icon class="nav-icon"><component :is="link.icon" /></el-icon>
            {{ link.label }}
          </el-link>
        </div>

        <!-- 右侧用户操作 -->
        <div class="user-actions">
          <template v-if="!userStore.isAuthenticated">
            <el-button
                size="small"
                type="primary"
                class="login-btn"
                @click="goToLogin"
            >
              登录
            </el-button>
            <el-button
                size="small"
                plain
                class="register-btn"
                @click="goToRegister"
            >
              注册
            </el-button>
          </template>
          <template v-else>
            <el-dropdown
                trigger="hover"
                v-model:visible="userMenuVisible"
                @command="(cmd) => { if(cmd==='logout') handleLogout(); else router.push(cmd); }"
            >
              <span class="el-dropdown-link" style="color:#fff;cursor:pointer;display:flex;align-items:center;">
                <el-icon style="margin-right:4px;"><UserIcon /></el-icon>
                {{ userStore.userName }}
                <el-icon style="margin-left:2px;"><i class="el-icon-arrow-down"></i></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="/profile">个人中心</el-dropdown-item>
                  <el-dropdown-item command="/profile/orders">我的订单</el-dropdown-item>
                  <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
          <el-button
              v-if="userStore.isSeller"
              type="primary"
              @click="goSellerCenter"
              style="margin-right: 10px;"
          >
            卖家中心
          </el-button>
        </div>
      </div>
    </div>

    <!-- 搜索区域 -->
    <div class="search-header">
      <div class="container">
        <div class="header-content">
          <!-- Logo区域 -->
          <div class="logo-section">
            <el-icon class="logo-icon"><ShoppingCart /></el-icon>
            <div>
              <div class="logo-text">在线商城系统</div>
              <div class="logo-tagline">品质生活 · 轻松购物</div>
            </div>
          </div>

          <!-- 搜索区域居中 -->
          <div class="search-center">
            <div class="search-section">
              <div class="search-box">
                <el-input
                    v-model="searchKeyword"
                    placeholder="搜索商品名称、品牌或分类..."
                    clearable
                    class="search-input"
                    @keyup.enter="handleSearch"
                >
                  <template #prefix>
                    <el-icon class="search-icon"><Search /></el-icon>
                  </template>
                </el-input>
                <el-button
                    class="search-button"
                    @click="handleSearch"
                >
                  搜索
                </el-button>
              </div>
              <div class="search-tips">
                热门搜索：
                <el-tag
                    v-for="tag in hotTags"
                    :key="tag"
                    class="hot-tag"
                    @click="searchKeyword = tag; handleSearch();"
                >
                  {{ tag }}
                </el-tag>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, markRaw } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/userStore';
import { ElMessage } from 'element-plus';
import {
  House,
  List,
  ShoppingCart,
  Document,
  User as UserIcon,
  Search
} from '@element-plus/icons-vue';

const router = useRouter();
const userStore = useUserStore();
const activeLink = ref('home');
const searchKeyword = ref('');
const userMenuVisible = ref(false);

const navLinks = ref([
  { name: 'home', label: '首页', icon: markRaw(House), path: '/' },
  { name: 'products', label: '商品列表', icon: markRaw(List), path: '/products' },
  { name: 'cart', label: '购物车', icon: markRaw(ShoppingCart), path: '/cart' },
  { name: 'orders', label: '我的订单', icon: markRaw(Document), path: '/profile/orders' },
  { name: 'profile', label: '个人中心', icon: markRaw(UserIcon), path: '/profile' }
]);

const hotTags = ref(['华为', '苹果', '纯棉', '运动鞋', '桌子', '椅子']);

const handleNavClick = (name, path) => {
  activeLink.value = name;
  router.push(path);
};

const handleSearch = () => {
  if (searchKeyword.value.trim()) {
    router.push({
      name: 'SearchResults',
      query: { keyword: searchKeyword.value }
    });
  }
};

const goToLogin = () => {
  router.push('/login');
};

const goToRegister = () => {
  router.push('/register');
};

const handleLogout = () => {
  userStore.logout();
  ElMessage.success('已退出登录');
  router.push('/');
};

const goSellerCenter = () => {
  router.push('/seller/dashboard');
};
</script>

<style scoped>
.header-container {
  position: sticky;
  top: 0;
  z-index: 1000;
  background: white;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.top-navigation {
  background-color: #1a365d;
  height: 40px;
  display: flex;
  align-items: center;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.nav-links {
  display: flex;
  gap: 25px;
}

.nav-link {
  color: #cbd5e0;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  transition: all 0.3s ease;
  padding: 5px 0;
  position: relative;
}

.nav-link:hover {
  color: white;
}

.nav-link.active {
  color: white;
  font-weight: 600;
}

.nav-link.active::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 0;
  width: 100%;
  height: 3px;
  background: #ff6b35;
  border-radius: 3px;
}

.nav-icon {
  margin-right: 6px;
  font-size: 16px;
}

.user-actions {
  display: flex;
  gap: 12px;
}

/* 统一按钮样式 */
.login-btn,
.register-btn {
  padding: 6px 16px;
  height: 30px;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.3s ease;
}

.login-btn {
  background: #ff6b35;
  color: white;
  border: none;
}

.login-btn:hover {
  background: #e55a2b;
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(255, 107, 53, 0.3);
}

.register-btn {
  background: transparent;
  color: #cbd5e0;
  border: 1px solid #cbd5e0;
}

.register-btn:hover {
  color: white;
  border-color: white;
  background: rgba(255, 255, 255, 0.1);
}

.search-header {
  background-color: white;
  padding: 15px 0;
  border-bottom: 1px solid #eee;
}

.header-content {
  display: flex;
  align-items: center;
  width: 100%;
}

.logo-section {
  display: flex;
  align-items: center;
  min-width: 200px;
  gap: 12px;
  margin-right: 30px;
}

.logo-icon {
  font-size: 40px;
  color: #ff6b35;
}

.logo-text {
  font-size: 22px;
  font-weight: 700;
  color: #1a365d;
  line-height: 1.2;
}

.logo-tagline {
  font-size: 13px;
  color: #718096;
  letter-spacing: 1px;
}

.search-center {
  flex: 1;
  display: flex;
  justify-content: center;
}

.search-section {
  width: 100%;
  max-width: 700px;
}

.search-box {
  display: flex;
  margin-bottom: 8px;
}

.search-input {
  flex: 1;
  height: 46px;
  border-radius: 4px 0 0 4px;
  border: 2px solid #ff6b35;
  border-right: none;
  transition: all 0.3s ease;
}

.search-input:focus-within {
  box-shadow: 0 0 0 3px rgba(255, 107, 53, 0.2);
}

.search-icon {
  color: #a0aec0;
  font-size: 18px;
  margin-left: 10px;
}

.search-button {
  height: 46px;
  background: #ff6b35;
  color: white;
  border: none;
  border-radius: 0 4px 4px 0;
  padding: 0 24px;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
  box-shadow: 0 2px 4px rgba(255, 107, 53, 0.2);
}

.search-button:hover {
  background: #e55a2b;
  box-shadow: 0 2px 8px rgba(255, 107, 53, 0.3);
}

.search-tips {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #718096;
  flex-wrap: wrap;
  gap: 8px;
}

.hot-tag {
  cursor: pointer;
  transition: all 0.2s ease;
  padding: 4px 10px;
  border-radius: 4px;
}

.hot-tag:hover {
  background: #ff6b35;
  color: white;
}
</style>